<template>
  <el-dialog :title="!dataForm.id ? '收货检验非镜片类新增':'收货检验非镜片类修改'" :close-on-click-modal="false" :visible.sync="visible" center fullscreen>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
            <el-row :gutter="15">
        <el-col :span="8">
          <el-form-item label="制造商">
            <el-input v-model="dataForm.supplyName" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="收入仓位">
            <el-input v-model="dataForm.inStockName" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="收货单号">
            <el-input v-model="dataForm.billId" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-form :inline="true">
      <el-form-item><el-button type="primary" @click="openReceiveListDialog">按收货单添加商品</el-button></el-form-item>
      <el-form-item><el-button type="danger" @click="delTableData">删除</el-button></el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="goodsCode" label="商品编码"></el-table-column>
      <el-table-column prop="goodsName" label="商品名称"></el-table-column>
      <el-table-column prop="applyQuantity" label="订货数量"></el-table-column>
      <el-table-column prop="quantity" label="收货数量">
        <template slot-scope="scope">
          <el-input v-model="scope.row.quantity"  @input="changeGuantity(scope.row.quantity)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="conformRate" label="符合率"></el-table-column>
      <el-table-column prop="shape" label="外包装">
        <template slot-scope="scope">
          <el-select v-model="scope.row.shape">
            <el-option label="√" value="1"></el-option>
            <el-option label="×" value="0"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="checkFlag" label="标识">
        <template slot-scope="scope">
          <el-select v-model="scope.row.checkFlag">
            <el-option label="√" value="1"></el-option>
            <el-option label="×" value="0"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="qualifiedQuantity" label="合格数量">
        <template slot-scope="scope">
          <el-input v-model="scope.row.qualifiedQuantity" disabled></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="unqualifiedQuantity" label="不合格数量" min-width>
        <template slot-scope="scope">
          <el-input v-model="scope.row.unqualifiedQuantity" @input="changeGuantity(scope.row.unqualifiedQuantity)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="unqualifiedRate" label="不合格率">
        <template slot-scope="scope">
          <el-input v-model="scope.row.unqualifiedRate" disabled></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="memo" label="验收不合格事项及处置措施">
        <template slot-scope="scope">
          <el-input v-model="scope.row.memo"></el-input>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>保存</el-button>
      <el-switch v-model="dataForm.applyStatus" active-text="保存并审核" inactive-value="2" active-value="1" style="margin-left: 15px;"></el-switch>
    </span>
    <!-- 弹窗, 收购订单列表查询 -->
    <receive-list v-if="receiveListVisible" ref="receiveList" @reshFormData="updateFormData"></receive-list>
  </el-dialog>
</template>

<script>
    import {saveNewCheckOrder,saveAndAuditOrder,getStockDataList,getCtgDataList} from '@/api/eims/check'
    import {allocationApplyGetObj,allocationApplyEdit} from '@/api/eims/allocation-apply'
    import {receiveTestAddObj } from '@/api/eims/purchase/receive-test'
    import { getInventoryDetailInfo } from '@/api/eims/purchase/receive-goods'
    import {fetchTree} from '@/api/admin/dept.js'
    import ReceiveList from './receive-list.vue'
    export default {
    data () {
      return {
				ischeck: false,
        visible: false,
        dataForm: {
          id: 0,
          applyStatus: '2',
          outDeptId: '',
          outDeptName: '',
          inDeptId: '',
          inDeptName: '',
          memo: '',
          detailList: []
        },
        deptOptions: [],
        dataList: [],
        dataListLoading: false,
        dataRule: {outDeptId: [{ required: true,message: '请选择申请部门'}],inDeptId: [{required:true,message: '请选择接收部门'}]},
        selectionList: [],
        receiveListVisible: false
      }
    },
    components: { ReceiveList },
    methods: {
      init (id) {
        this.getDeptList()
        this.dataForm.id = id || 0
        this.visible = true
        this.dataList = []
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.dataListLoading = true
            allocationApplyGetObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data.data
                let goodsData = response.data.data.data.detailList
                this.dataList = goodsData
                for(let i = 0; i < this.dataList.length; i++){
                  this.dataList[i].goodsBarcode = goodsData[i].barcode
                  this.dataList[i].goodsName = goodsData[i].name
                  this.dataList[i].spec = goodsData[i].spec
                }
              }else{
                this.$message.error(response.data.msg)
              }
              this.dataListLoading = false
            })
          }
        })
      },
      outDeptChange($event) {
        this.deptOptions.forEach(item => { if(item.value == $event){ this.dataForm.outDeptName = item.name } })
      },
      inDeptChange($event) {
        this.deptOptions.forEach(item => { if(item.value == $event){ this.dataForm.inDeptName = item.name } })
      },
      // 打开商品列表弹窗
      openReceiveListDialog() {
        this.receiveListVisible = true
        this.$nextTick(() => {  this.$refs.receiveList.init(1) })
      },
      handleSelectionChange(val) {
        this.selectionList = val
      },
      // 删除选中的商品
      delTableData() {
        this.selectionList.forEach((item,index) => {
          for(let i = 0; i < this.dataList.length; i++){
            if(this.dataList[i] == item){
              this.dataList.splice(i,1)
            }
          }
          this.selectionList = []
        })
      },
      //更新表单数据
      updateFormData(obj) {
        this.dataList = []
        this.dataForm.linkBillId = obj.billId
        this.dataForm.inStockId = obj.inStockId
        this.dataForm.inStockName = obj.inStockName
        this.dataForm.supplyId = obj.supplyId
        this.dataForm.supplyName = obj.supplyName
        getInventoryDetailInfo(obj.billId).then(response => {
          if(response.data.code == '0'){
            let list = response.data.data
            let list1 = list.map(item => {
              item.shape = '1'
              item.checkFlag = "1"
              item.unqualifiedQuantity = 0
              item.qualifiedQuantity = item.quantity
              item.unqualifiedRate = "0.00%"
              //写死的  订货数量 符合率
              item.conformRate = "100.00%"
              item.applyQuantity = 0
              return item
            })
            this.dataList = [...list1]
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //商品数量修改
      changeGuantity(data){
       this.dataList.forEach(item => {
          item.qualifiedQuantity =item.quantity-item.unqualifiedQuantity;
          item.unqualifiedRate = (item.unqualifiedQuantity*100 / item.quantity).toFixed(2) +"%"
        })
        this.dataList = [...this.dataList]
     },
      // 表单提交
      dataFormSubmit () {
				this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dataForm.detailList = this.dataList
            this.dataForm.checkType = "0"
            receiveTestAddObj(this.dataForm).then(response => {
              if(response.data.code == '0'){
                (this.dataForm.applyStatus == '1') ? this.$message.success('新增并审核成功') : this.$message.success('新增成功')
                this.visible = false
                this.$emit('refreshDataList')
              }else{
                this.$message.error(response.data.msg)
              }
              this.ischeck = false
            }).catch(() => {
							this.ischeck = false
						})
          }else{
						this.ischeck = false
					}
        })
      },
      //获取所需数据字典
     getDeptList(){
       fetchTree().then(response => {
         if(response.data.code == '0'){
           let dataList = response.data.data
           let options = []
           dataList.forEach(item => {
             options.push({value:item.id,name:item.name})
           })
           this.deptOptions = options
         }else{
           this.$message.error(response.data.msg)
         }
       })
     }
    }
  }
</script>
